Skip to main content
Version: 1.0.16

CREATE EVENT TRIGGER

CREATE EVENT TRIGGER — Define a new event trigger

Synopsis

CREATE EVENT TRIGGER name

ON event

[ WHEN filter_variable IN (filter_value [, ... ]) [ AND ... ] ]

EXECUTE { FUNCTION | PROCEDURE } function_name()

Description

CREATE EVENT TRIGGER creates a new event trigger. Whenever the specified event occurs and the WHEN conditions associated with the trigger (if any) are satisfied, the trigger's function will be executed. The user who creates the event trigger becomes its owner.

Parameters

name

The name of the new trigger. This name must be unique within the database.

event

The name of the event that triggers a call to the given function.

filter_variable

The name of a variable used to filter events. This can be used to restrict the trigger to fire only for a subset of cases it supports. Currently, the only supported filter_variable is TAG.

filter_value

A list of values associated with the filter_variable for which the trigger should fire. For TAG, this represents a list of command tags (e.g., 'DROP FUNCTION').

function_name

A user-supplied function that is declared to take no arguments and return type event_trigger.

In the syntax of CREATE EVENT TRIGGER, the keywords CREATE EVENT TRIGGER and PROCEDURE are equivalent, but the referenced function must in any case be a function, not a procedure. The use of the keyword PROCEDURE here is historical and deprecated.

Notes

Only superusers can create event triggers.

Event triggers are disabled in single-user mode. If a faulty event trigger disables the database to the point where you cannot even drop it, you can restart in single-user mode to remove it.

Examples

# Forbid the execution of any DDL command:

CREATE OR REPLACE FUNCTION abort_any_command()

RETURNS event_trigger

LANGUAGE plpgsql

AS $$

BEGIN

RAISE EXCEPTION 'command % is disabled', tg_tag;

END;

$$;

CREATE EVENT TRIGGER abort_ddl ON ddl_command_start

EXECUTE FUNCTION abort_any_command();

See Also

ALTER EVENT TRIGGER, DROP EVENT TRIGGER, CREATE FUNCTION